package com.ankur.fareplease;

import java.text.NumberFormat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.ankur.fareplease.filters.DecimalDigitsInputFilter;
import com.ankur.fareplease.model.FareDAO;
import com.ankur.fareplease.service.TaxiFareService;
import com.ankur.fareplease.service.impl.TaxiFareServiceImpl;

public class TaxiActivity extends Activity {
	private NumberFormat numFormat;

	private Spinner citySelect;
	private Spinner cabSelect;
	private CheckBox night_charge_check;
	private EditText kms_input;
	private TextView final_fare;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		numFormat = NumberFormat.getInstance();
		numFormat.setMaximumFractionDigits(2);
		numFormat.setMinimumFractionDigits(2);
		setContentView(R.layout.taxiinput);

		citySelect = (Spinner) findViewById(R.id.taxi_city_select);
		cabSelect = (Spinner) findViewById(R.id.taxi_cab_select);
		night_charge_check = (CheckBox) findViewById(R.id.taxi_night_charge_check);
		kms_input = (EditText) findViewById(R.id.taxi_kms_input);
		kms_input
				.setFilters(new InputFilter[] { new DecimalDigitsInputFilter(1) });
		final_fare = (TextView) findViewById(R.id.taxi_final_fare);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.taxi_city_array,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
		citySelect.setAdapter(adapter);
		citySelect
				.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> adapterView,
							View view, int i, long l) {
						String selectedCity = adapterView.getSelectedItem()
								.toString();
						if (selectedCity.equals("Pune")) {
							ArrayAdapter<CharSequence> puneAdapter = ArrayAdapter
									.createFromResource(
											TaxiActivity.this,
											R.array.pune_taxi_array,
											android.R.layout.simple_spinner_item);
							puneAdapter
									.setDropDownViewResource(android.R.layout.simple_spinner_item);
							cabSelect.setAdapter(puneAdapter);
						} else if (selectedCity.equals("Mumbai")) {
							ArrayAdapter<CharSequence> mumbaiAdapter = ArrayAdapter
									.createFromResource(
											TaxiActivity.this,
											R.array.mumbai_taxi_array,
											android.R.layout.simple_spinner_item);
							mumbaiAdapter
									.setDropDownViewResource(android.R.layout.simple_spinner_item);
							cabSelect.setAdapter(mumbaiAdapter);
						}
					}

					@Override
					public void onNothingSelected(AdapterView<?> adapterView) {

					}
				});

		Button getFareButton = (Button) findViewById(R.id.taxi_fare_button);
		getFareButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				FareDAO fareDAO = new FareDAO();
				fareDAO.setCity((String) citySelect.getSelectedItem());
				fareDAO.setCabService((String)cabSelect.getSelectedItem());
				fareDAO
						.setKms(Float
								.parseFloat(kms_input.getText().toString()));
				fareDAO.setNightFare(night_charge_check.isChecked());
				TaxiFareService service = new TaxiFareServiceImpl();
				final_fare.setText(numFormat.format(service.getFare(fareDAO)));
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.about:
			startActivity(new Intent(this, AboutActivity.class));
			break;
		case R.id.exit:
			finish();
			break;
		}
		return true;
	}
}
